package org.example.day;

/**
 * @Description: Solution2288
 * @Author wyatt
 * @Data 2024/06/18 12:14
 */
public class Solution2288 {

    public static void main(String[] args) {
        Solution2288 solution2288 = new Solution2288();
        System.out.println(solution2288.discountPrices("$2.00", 50));
    }

    /**
     * @Description:
     * 1. 根据空格切分为单词数组
     * 2. 判断是否是金额
     * 3. 针对金额进行转换
     * 4. 拼接字符串
     * @auther: wyatt
     * @date: 12:16 2024/6/18
     */
    public String discountPrices(String sentence, int discount) {
        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (word.charAt(0) == '$' && isNumeric(word.substring(1))) {
                double price = Long.parseLong(word.substring(1)) * (1 - discount / 100.0);
                words[i] = String.format("$%.2f", price);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            if (i > 0) {
                sb.append(" ");
            }
            sb.append(words[i]);
        }
        return sb.toString();
    }

    public boolean isNumeric(String s) {
        if (s.isEmpty()) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }


}
